package com.foreveross.action.weixin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.DDManagerService;
import com.foreveross.service.ExpressService;
import com.foreveross.service.HrHouseInfoService;
import com.foreveross.service.LogService;
import com.foreveross.util.AuthUtil;
import com.foreveross.util.DateUtil;
import com.foreveross.vo.Express;
import com.foreveross.vo.ExpressCompany;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.HrCode;
import com.foreveross.vo.HrNewsInfo;
import com.foreveross.vo.Tenement;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/weixin/expressController")
public class ExpressController {

	private Logger log = Logger.getLogger(ExpressController.class);

	@Resource
	private ExpressService expressService;

	@Resource
	private DDManagerService ddManagerService;

	/**
	 * 快递信息查询
	 * 
	 * @param request
	 * @param response
	 * @param express_no
	 * @param name
	 * @param type
	 * @param status
	 * @param house_id
	 * @param OnePageCount
	 * @param currentPage
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getExpress")
	public void getExpress(HttpServletRequest request,
			HttpServletResponse response, String weixinId, String type,String currentPage,String pageSize) {
		ResultVo resultVo = new ResultVo();
		try {
			expressService.findMyExpressList(weixinId, type, currentPage,
					pageSize, resultVo);
			log.info("快递信息 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("快递信息  查看失败");
			log.error("快递信息 查看失败!", e);
		}
		List list = Arrays.asList("houseUser");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo, list);
	}
	
	
	/**
	 * 查看未处理的快递总数
	 * @param request
	 * @param response
	 * @param weixinId
	 * @param type
	 * @param currentPage
	 * @param pageSize
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findUntreatedExpressTotal")
	public void findUntreatedExpressTotal(HttpServletRequest request,
			HttpServletResponse response, String weixinId, String type,String currentPage,String pageSize) {
		ResultVo resultVo = new ResultVo();
		try {
		    int untreatedExpressTotal=expressService.findUntreatedTotal(weixinId, type, currentPage, pageSize);
			resultVo.setSuccess(true);
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("untreatedExpressTotal", untreatedExpressTotal);
			resultVo.setData(map);
			log.info("未处理的快递信息 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("未处理的快递信息  查看失败");
			log.error("未处理的快递信息 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	

	/**
	 * 快递信息添加
	 * 
	 * @param request
	 * @param response
	 * @param express
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/addExpress")
	public void addExpress(HttpServletRequest request,
			HttpServletResponse response, Express express,boolean isAgree) {
		ResultVo resultVo = new ResultVo();
		try {
			expressService.addExpress(express,isAgree);
			resultVo.setSuccess(true);
			log.info("快递信息 " + Constants.UPDATE_ADD + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("快递信息 " + Constants.UPDATE_ADD + "失败");
			log.error("快递信息" + Constants.UPDATE_ADD + "失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 签订快递协议
	 * 
	 * @param request
	 * @param response
	 * @param express
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/isAgreeExpress")
	public void isAgreeExpress(HttpServletRequest request,
			HttpServletResponse response, String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			expressService.isAgreeExpress(userId);
			resultVo.setSuccess(true);
			log.info("签订快递协议 " + Constants.UPDATE_ADD + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("签订快递协议 " + Constants.UPDATE_ADD + "失败");
			log.error("签订快递协议" + Constants.UPDATE_ADD + "失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 快递信息删除
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteExpress")
	public void deleteExpress(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			expressService.deleteObjec("Express", id);
			resultVo.setSuccess(true);
			log.info("快递信息 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("快递信息 删除失败");
			log.info("快递信息 删除失败!");
			e.printStackTrace();
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 获取快递公司信息
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getExpressCompany")
	public void getExpressCompany(HttpServletRequest request,
			HttpServletResponse response) {
		ResultVo resultVo = new ResultVo();
		try {
			ddManagerService.findAllExpressCompany(resultVo);
			log.info("获取快递公司信息成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("快递公司信息获取失败！");
			log.error("快递公司信息获取失败！", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
